package com.ooqiu.gaming.components.route_interceptor;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ooqiu.gaming.GamingApp;
import com.ooqiu.gaming.common.Constants;
import com.ooqiu.gaming.components.storage.UserStorage;
import com.ooqiu.gaming.ui.login.LoginActivity;
import com.ooqiu.gaming.utils.LogUtil;

/**
 * @author Eenie
 * @date 2017/11/21
 */

@Interceptor( priority = 8)
public class LoginInterceptor implements IInterceptor {
    private Context mContext;
    private UserStorage mUserStorage;
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {


        if (postcard.getExtra() == Constants.ROUTER_EXTRA_NEED_LOGIN && !mUserStorage.isLogin()) {
            callback.onInterrupt(new IllegalStateException("the user is null,need to loginCheck"));
            ARouter.getInstance().build(LoginActivity.PATH).greenChannel().navigation();
        } else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {

        LogUtil.e("LoginInterceptor");

        this.mContext = context;
        mUserStorage = ((GamingApp) context).getApplicationComponent().getUserStorage();
    }
}
